package com.zide.cms.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zide.cms.domain.po.CmsBanner;
import com.zide.cms.domain.vo.result.CmsBannerResultVO;
import com.zide.cms.domain.vo.save.CmsBannerSaveVO;
import com.zide.cms.domain.vo.search.CmsBannerSearchVO;
import com.zide.cms.mapper.CmsBannerMapper;
import com.zide.cms.service.ICmsBannerService;
import com.zide.common.core.domain.PageResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.zide.common.core.vo.CommonParamIdVO;
import com.zide.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 轮播图管理Service业务层处理
 * 
 * @author zide
 * @date 2024-05-06
 */
@Slf4j
@Service
public class CmsBannerServiceImpl implements ICmsBannerService {
    @Autowired
    private CmsBannerMapper cmsBannerMapper;

    /**
     * 查询轮播图管理列表
     */
    @Override
    public PageResult<CmsBannerResultVO> searchCmsBannerPageList(CmsBannerSearchVO searchVO) {
        PageResult<CmsBanner> dbPageResult = cmsBannerMapper.selectPage(searchVO);
        List<CmsBannerResultVO> resultList = BeanUtil.copyToList(dbPageResult.getList(),CmsBannerResultVO.class);
        PageResult<CmsBannerResultVO> result = new PageResult<>();
        result.setTotal(dbPageResult.getTotal());
        result.setList(resultList);
        return result;
    }

    /**
     * 查询轮播图管理详情
     */
    @Override
    public CmsBannerResultVO selectCmsBannerById(Long id) {
            CmsBanner dbInfo = cmsBannerMapper.selectById(id);
            CmsBannerResultVO result = BeanUtil.copyProperties(dbInfo,CmsBannerResultVO.class);
        return result;
    }

    /**
     * 新增轮播图管理
     */
    @Override
    public int saveCmsBanner(CmsBannerSaveVO saveVO) {
        CmsBanner insertBean = BeanUtil.copyProperties(saveVO,CmsBanner.class);
        return cmsBannerMapper.insert(insertBean);
    }

    /**
     * 修改轮播图管理
     */
    @Override
    public int updateCmsBanner(CmsBannerSaveVO saveVO) {
        CmsBanner updateBean = BeanUtil.copyProperties(saveVO,CmsBanner.class);
        return cmsBannerMapper.updateById(updateBean);
    }

    /**
     * 批量删除轮播图管理
     */
    @Override
    public int removeCmsBannerByIds(CommonParamIdVO idVO) {
        if(ArrayUtil.isNotEmpty(idVO.getIds())) {
            return cmsBannerMapper.deleteBatchIds(Arrays.asList(idVO.getIds()));
        }else {
            return cmsBannerMapper.deleteById(idVO.getId());
        }

    }

    /**
     * 查看对应页面中的banner图
     */
    @Override
    public List<CmsBannerResultVO> searchPageBanner(String page) {
        log.info("page====>{}",page);
        LambdaQueryWrapper<CmsBanner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsBanner::getShowPage,page);
        List<CmsBanner> cmsBanners = cmsBannerMapper.selectList(wrapper);
        return BeanUtil.copyToList(cmsBanners,CmsBannerResultVO.class);
    }
}
